﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoolCode;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Web.Mvc;
using CoolCode.Linq;
using Kangxin.Mvc.Models;
using System.Collections.Specialized;

namespace Kangxin.Mvc.Controllers
{
    public class TestController : CoolCode.Web.Mvc.ControllerBase
    {
        public ActionResult CheckBoxListTest()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem { Selected = false, Text = "Role1", Value = "Role1" });
            items.Add(new SelectListItem { Selected = true, Text = "Role2", Value = "Role2" });
            items.Add(new SelectListItem { Selected = false, Text = "Role3", Value = "Role3" });
            items.Add(new SelectListItem { Selected = true, Text = "Role4", Value = "Role4" });
            SelectList list = new SelectList(items, "Value", "Text");
            UserRoleModel model = new UserRoleModel
            {
                UserName = "Bruce",
                Roles = items//list
            };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveUser()
        {
            var form = this.HttpContext.Request.Form;
            UserRoleModel model = new UserRoleModel();
            TryUpdateModel(model);
            string key = model.UserName;
            if (model.SelectedRoles == null)
            {
                return this.Message(false, "失败");
            }
            return this.Message(true, "成功");
        }

        #region Grid

        public ActionResult GridTest(int? page)
        {
            var list = ListLoginModel(page);
            return View(list);
        }

        public ActionResult GridParital(int? page)
        {
            System.Threading.Thread.Sleep(new Random().Next(200,2300));
            var list = ListLoginModel(page);
            return View(list);
        }

        public ActionResult Grid2Test()
        {
            return GridTest(0);
        }


        public ActionResult Grid3Test( )
        { 
            return View();
        }

        #endregion

        #region Pager

        public ActionResult PagerTest(int? page, int? page2, int? page3)
        {
            int pageIndex = page.GetValueOrDefault();
            ViewData["PagerSource"] = new FooPaginatedList(pageIndex, 15, 102);
            int pageIndex2 = page2.GetValueOrDefault();
            ViewData["PagerSource2"] = new FooPaginatedList(pageIndex2, 15, 203);
            int pageIndex3 = page3.GetValueOrDefault();
            ViewData["PagerSource3"] = new FooPaginatedList(pageIndex3, 10, 214);
            return View();
        }

        public ActionResult Pager2Test(int? page, int? page2, int? page3)
        {
            //int pageIndex = page.GetValueOrDefault();
            //var model = new PaginatedList<Foo>(Foo(),pageIndex, 15);
            //return View(model);
            return View();
        }

        public ActionResult Pager2Part1(int? page, int? page2, int? page3)
        {
            int pageIndex = page.GetValueOrDefault();
            var model = new PaginatedList<Foo>(Foo(), pageIndex, 15);
            return View(model);
        }

        public ActionResult Pager2Part2(int? page, int? page2, int? page3)
        {
            int pageIndex = page.GetValueOrDefault();
            var model = new PaginatedList<Foo>(Foo(), pageIndex, 10);
            return View(model);
        }

        public ActionResult Pager2Part3(int? page, int? page2, int? page3)
        {
            int pageIndex = page.GetValueOrDefault();
            var model = new PaginatedList<Foo>(Foo(), pageIndex, 9);
            return View(model);
        }

        #endregion

        #region FluentHtml 

        public ActionResult FluentHtmlTest()
        {
            //List<SelectListItem> items = new List<SelectListItem>();
            //items.Add(new SelectListItem { Selected = false, Text = "Role1", Value = "Role1" });
            //items.Add(new SelectListItem { Selected = true, Text = "Role2", Value = "Role2" });
            //items.Add(new SelectListItem { Selected = false, Text = "Role3", Value = "Role3" });
            //items.Add(new SelectListItem { Selected = true, Text = "Role4", Value = "Role4" });
            //SelectList list = new SelectList(items, "Value", "Text");
         
            //UserRoleModel model = new UserRoleModel
            //{
            //    UserName = "Bruce",
            //    Roles = items//list
            //};
            UserModel model = new UserModel();
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FluentHtmlTest(NameValueCollection param)
        {
            UserModel model = new UserModel();
            
            TryUpdateModel(model);
            bool isvalid = ModelState.IsValid;
            ViewData["Result"] =string.Join(",", param.AllKeys ) +param ;
            return View(model );
        }

        #endregion

        #region Helper

        private static IEnumerable<Foo> Foo()
        {
            Random rand = new Random();
            int count = rand.Next(103, 116);
            for (int i = 0; i < count; i++)
                yield return new Foo { Name = "Bruce" + i, Num = i + 1 };
        }

        internal class FooPaginatedList : IPaginatedList
        {
            public FooPaginatedList(int pageIndex, int pageSize, int totalRecords)
            {
                PageIndex = pageIndex;
                PageSize = pageSize;
                TotalRecords = totalRecords;
            }

            #region IPaginatedList Members

            public int PageIndex
            {
                get;
                private set;
            }

            public int PageSize
            {
                get;
                private set;
            }

            public int TotalRecords
            {
                get;
                private set;
            }


            public bool HasPreviousPage
            {
                get
                {
                    return (PageIndex > 0);
                }
            }

            public bool HasNextPage
            {
                get
                {
                    return (PageIndex + 1 < PageCount);
                }
            }

            public int PageCount
            {
                get
                {
                    return (int)Math.Ceiling(TotalRecords / (double)PageSize);
                }
            }

            #endregion
        }

        private PaginatedList<LoginModel> ListLoginModel(int? page)
        {
            int pageIndex = page.GetValueOrDefault();
            var model = Enumerable.Range(1, 124).Select(i => new LoginModel
            {
                UserName = "Bruce" + i,
                Password = "P" + i,
                RememberMe =( i%5==0)  ,
                SubTitle = "SubTitle"+i
            });
            //List<LoginModel> model = new List<LoginModel>();
            //model.Add(new LoginModel { UserName = "Bruce", Password = "12 3", RememberMe = true, SubTitle = "22" });
            //model.Add(new LoginModel { UserName = "Bruce 士大夫", Password = "123中文", RememberMe = true, SubTitle = "h" });
            //model.Add(new LoginModel { UserName = "Bruce 董事长", Password = "123", RememberMe = false, SubTitle = "222" });
            //model.Add(new LoginModel { UserName = "<b>Bruce</b> ", Password = "12#%$&%&3", RememberMe = true, SubTitle = "" });
            //model.Add(new LoginModel { UserName = "Bru ce sd ", Password = "123$$$$", RememberMe = false });
            //model.Add(new LoginModel { UserName = "Bru ce sd ", Password = "123$$$$", RememberMe = false });
            //model.Add(new LoginModel { UserName = "Bru k ce sd ", Password = "134$$$$", RememberMe = false });
            //model.Add(new LoginModel { UserName = "Bru z ce sd ", Password = "123$$$$", RememberMe = false });

            PaginatedList<LoginModel> list = new PaginatedList<LoginModel>(model, new PageParam(pageIndex, 10));
            return list;
        }

        #endregion

        public  ActionResult CheckBoxPostTest()
        {
            var model =new {c= new List<object>{
             true,false ,true,false ,true 
            }};
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CheckBoxPostTest(NameValueCollection form)
        {
            var f=form;
            return View();
        }
    }
}
